<template>
  <div class="content">
    <el-card class="header">
      <!-- 头部 -->
      <div slot="header">
        <my-bread>内容管理</my-bread>
      </div>
      <el-form label-width="80px">
        <el-form-item label="状态">
          <el-radio-group v-model="from.status">
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="频道">
          <el-select
            v-model="from.channel_id"
            placeholder="请选择"
            :clearable="true"
            @clear="reset"
          >
            <el-option
              v-for="item in channels"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="日期">
          <el-date-picker
            v-model="dataArr"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="changTime"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="body">
      <div>根据筛选条件查询到{{ total }}条数据,当前是第{{ from.page }}页：</div>
      <el-table :data="articleList" style="width: 100%" v-loading="loading">
        <el-table-column prop="date" label="封面">
          <template slot-scope="scope">
            <el-image :src="scope.row.cover.images[0]" class="cover-image">
              <div slot="error" class="image-slot">
                <img src="../../assets/error.gif" class="cover-image" />
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题"> </el-table-column>
        <el-table-column prop="address" label="状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === 0">草稿</el-tag>
            <el-tag v-if="scope.row.status === 1">待审核</el-tag>
            <el-tag v-if="scope.row.status === 2" type="success"
              >审核通过</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="pubdate" label="发布时间"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              size="mini"
              @click="edit(scope.row.id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
              @click="del(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="prev, pager, next"
        style="margin-top: 10px"
        :total="total"
        :current-page="from.page"
        :page-size="from.per_page"
        @current-change="onPageChange"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
// 文章列表、文章频道、删除文章
import { getContent, getChannel, delArticle } from '@/api/content'
export default {
  name: 'content',
  data () {
    return {
      dataArr: [],
      from: {
        status: null,
        page: 1,
        per_page: 10,
        channel_id: null,
        begin_pubdate: null,
        end_pubdate: null
      },
      total: 0,
      articleList: [],
      channels: [],
      loading: true
    }
  },
  created () {
    this.getContentList()
    this.getChannels()
  },
  methods: {
    async getContentList () {
      const {
        data: { data }
      } = await getContent(this.from)
      console.log(data)
      this.articleList = data.results
      this.total = data.total_count
      this.loading = false
    },
    async getChannels () {
      const {
        data: { data }
      } = await getChannel()
      console.log(data)
      this.channels = data.channels
    },
    search () {
      this.loading = true
      this.from.page = 1
      this.getContentList()
    },
    onPageChange (newPage) {
      this.from.page = newPage
      this.getContentList()
    },
    // 时间选择
    changTime (dataTime) {
      console.log(dataTime)
      if (dataTime) {
        this.from.begin_pubdate = dataTime[0]
        this.from.end_pubdate = dataTime[1]
      } else {
        this.from.begin_pubdate = null
        this.from.end_pubdate = null
      }
    },
    // 删除文章
    async del (id) {
      console.log(id)
      this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          try {
            // 删除请求
            await delArticle(id)
            this.$message.success('删除成功!')
            this.getContentList()
          } catch (e) {
            this.$message.error('删除失败')
          }
        })
        .catch(() => {})
    },
    // 编辑文章
    edit (id) {
      this.$router.push({ path: '/addArticle', query: { id } })
    },
    reset () {
      this.from.channel_id = null
    }
  }
}
</script>

<style lang="less">
.content {
  .header {
    border-bottom: 1px solid #fff;
  }
  .body {
    margin-top: 20px;
  }
  .cover-image {
    width: 180px;
    height: 120px;
  }
}
</style>
